import requests
import json


def certified_models():
    # 1. /dcl/compliance/certified-models
    # Queries a list of CertifiedModel items
    url = 'https://on.dcl.csa-iot.org/dcl/compliance/certified-models?pagination.count_total=true&pagination.reverse=true'
    cert_models = json.loads(requests.get(url).content.decode('utf-8'))
    return cert_models


def certified_model_by_index(vid, pid, softwareVersion, cert_type):
    # /dcl/compliance/certified-models/{vid}/{pid}/{softwareVersion}/{certificationType}
    # Queries a CertifiedModel by index.
    # vid/pid/softwareVersion: int32, certificationType: string
    url = f'https://on.dcl.csa-iot.org/dcl/compliance/certified-models/{vid}/{pid}/{softwareVersion}/{cert_type}'
    cert_model = json.loads(requests.get(url).content.decode('utf-8'))
    return cert_model


def vendorInfo(vid):
    vid_format_error = 'vendorID should be int32'
    if type(vid) != int:
        return vid_format_error
    url = f'https://on.dcl.csa-iot.org/dcl/vendorinfo/vendors/{vid}'
    response = requests.get(url).content.decode('utf-8')
    res_json = json.loads(response)
    if res_json['code'] == 5:
        return 'not found'
    else:
        return res_json['vendorInfo']


if __name__ == '__main__':
    print(vendorInfo(0x1118))
